package myapp.framelibrary.db;

import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import java.io.File;

/**
 * Created by 超 on 2017/6/27.
 */

public class DaoSupportFactory {

    private static DaoSupportFactory mDaoSupportFactory;
    private SQLiteDatabase mSqliteDatabase;
    private DaoSupportFactory(){
        File dbRoot = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.pathSeparator+"myapp"+File.pathSeparator+"database");
        if (!dbRoot.exists()){
            dbRoot.mkdirs();
        }
        File dbFile = new File(dbRoot,"myapp.db");
        mSqliteDatabase = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
    }

    public static DaoSupportFactory getFactroy(){
        mDaoSupportFactory = new DaoSupportFactory();
        if (mDaoSupportFactory==null){
            synchronized (DaoSupportFactory.class){
                if (mDaoSupportFactory==null){
                    mDaoSupportFactory = new DaoSupportFactory();
                }
            }
        }

        return mDaoSupportFactory;
    }
    public <T >IDaoSupport<T> getDao(Class<T> clazz ){
        IDaoSupport i =new DaoSupport();
        i.init(mSqliteDatabase,clazz);
        return i;
    }
}
